import 'package:flutter/material.dart';
import 'package:flutter_default_project/state_container.dart';
import 'package:flutter_default_project/widgets/todo_item.dart';

class TodoList extends StatelessWidget {
  const TodoList({super.key});

  @override
  Widget build(BuildContext context) {
    final stateContainer = StateContainer.of(context);
    final todos = stateContainer.todos;
    if (stateContainer.state.isLoading) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            CircularProgressIndicator(),
            SizedBox(height: 10),
            Text(
              'Loading...',
              style: TextStyle(color: Theme.of(context).primaryColor),
            ),
          ],
        ),
      );
    }
    return ListView.builder(
      itemBuilder: (context, index) {
        final todo = todos[index];
        return TodoItem(
          todo: todo,
          onChanged: (value) {
            StateContainer.of(context).updateTodo(todo, isCompleted: value);
          },
        );
      },
      itemCount: todos.length,
    );
  }
}
